﻿using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
using Xfm.TagHelpers.Enums;
using Xfm.TagHelpers.Exts;

namespace Xfm.TagHelpers
{
    /// <summary>
    /// 自定义Card
    /// </summary>
    public class CardTagHelper : TagHelper
    {
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 颜色
        /// </summary>
        public ThemeColor Theme { get; set; }
        public bool OutLine { get; set; }
        /// <summary>
        /// title前面的图标
        /// </summary>
        public string Icon { get; set; }
        /// <summary>
        /// 自带的Tools，可选refresh,maximize,collapse,remove 多个用逗号分隔
        /// </summary>
        public string Tools { get; set; }
        /// <summary>
        /// tools里必须包含refresh，刷新异步加载url内容的body
        /// </summary>
        public string RefreshUrl { get; set; }
        /// <summary>
        /// tools里必须包含refresh，生成的refresh button元素的id
        /// </summary>
        public string RefreshId { get; set; }
        /// <summary>
        /// Lodding状态
        /// </summary>
        //public string Overlay { get; set; }
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            TagHelperContent inner = await output.GetChildContentAsync();
            output.TagName = "div";
            output.AddClass("card").AddClass("card-" + Theme.ToString().ToLower(CultureInfo.CurrentCulture));
            if (OutLine == true)
                output.AddClass("card-outline");
            if (!string.IsNullOrEmpty(Title) || !string.IsNullOrEmpty(Tools))
            {
                var head = CreateHeader();
                if (!string.IsNullOrEmpty(Title))
                    SetTitle(head);
                if (!string.IsNullOrEmpty(Tools))
                    SetTools(head);
                output.PreContent.SetHtmlContent(head);
            }
            TagBuilder body = new TagBuilder("div");
            body.AddCssClass("card-body");
            body.InnerHtml.AppendHtml(inner);
            output.Content.SetHtmlContent(body);
        }
        private TagBuilder CreateHeader()
        {
            TagBuilder head = new TagBuilder("div");
            head.AddCssClass("card-header");
            return head;
            //output.PreContent.SetHtmlContent(head);
        }
        private void SetTitle(TagBuilder head)
        {
            TagBuilder title = new TagBuilder("h3");
            title.AddCssClass("card-title");
            if (Icon != null)
            {
                var i = new TagBuilder("i");
                i.AddCssClass(Icon);
                title.InnerHtml.AppendHtml(i);
            }
            title.InnerHtml.Append(Title);
            head.InnerHtml.AppendHtml(title);
        }
        private void SetTools(TagBuilder head)
        {
            TagBuilder tools = new TagBuilder("div");
            tools.AddCssClass("card-tools");
            foreach (var item in Tools.Split(","))
            {
                TagBuilder bt = new TagBuilder("button");
                bt.Attributes.Add("type", "button");
                bt.AddCssClass("btn btn-tool");
                TagBuilder i = new TagBuilder("i");
                switch (item.ToLower())
                {
                    case "refresh":
                        bt.GenerateId(RefreshId, "-");
                        bt.Attributes.Add("data-source", RefreshUrl);
                        bt.Attributes.Add("data-card-widget", "card-refresh");
                        i.AddCssClass("fas fa-sync-alt");
                        break;
                    case "maximize":
                        bt.Attributes.Add("data-card-widget", "maximize");
                        i.AddCssClass("fas fa-expand");
                        break;
                    case "collapse":
                        bt.Attributes.Add("data-card-widget", "collapse");
                        i.AddCssClass("fas fa-minus");
                        break;
                    case "remove":
                        bt.Attributes.Add("data-card-widget", "remove");
                        i.AddCssClass("fas fa-times");
                        break;
                }
                bt.InnerHtml.AppendHtml(i);
                tools.InnerHtml.AppendHtml(bt);
            }
            head.InnerHtml.AppendHtml(tools);
        }
    }
}
